/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mobiubiq.directory.client.object;

import java.io.IOException;
import javax.microedition.contactless.ContactlessException;
import javax.microedition.contactless.TargetProperties;
import javax.microedition.contactless.TargetType;
import javax.microedition.contactless.ndef.NDEFMessage;
import javax.microedition.contactless.ndef.NDEFRecord;
import javax.microedition.contactless.ndef.NDEFRecordListener;
import javax.microedition.contactless.ndef.NDEFRecordType;
import javax.microedition.contactless.ndef.NDEFTagConnection;
import javax.microedition.contactless.rf.PlainTagConnection;
import javax.microedition.contactless.sc.ISO14443Connection;
import javax.microedition.io.Connector;

/**
 *
 * @author Marnix Renne
 */
public class TagObject extends AbstractObject implements NDEFRecordListener {

    private TargetProperties[] properties;
    private String tag;
    private String uri;

    public TagObject() {
        super("", ObjectType.TAG);
    }

    public TagObject(TargetProperties[] properties) {
        super(properties[0].getUid(), ObjectType.TAG);
        this.properties = properties;
        tag = properties[0].getUid();

        for (int i = 0; i < properties.length; i++) {
            NDEFTagConnection conn = null;
            try {
                Class[] connectionTypes = properties[i].getConnectionNames();
                String url = properties[i].getUrl();
                if (contains(connectionTypes, NDEFTagConnection.class)) {
                    conn = (NDEFTagConnection) Connector.open(url);
                    NDEFMessage message = conn.readNDEF();
                    NDEFRecordType[] types = message.getRecordTypes();
                    for (int j = 0; j < types.length; j++) {
                        NDEFRecord[] records = message.getRecord(types[j]);
                        for (int k = 0; k < records.length; k++) {
                            NDEFRecordType type = records[k].getRecordType();
                            byte[] payload = records[k].getPayload();
                            String name = type.getName();
                            if (name.equals("U")) { //urn:nfc:wkt:U
                                uri = new String(payload).trim();
                            }
//                        if(type.getFormat() == NDEFRecordType.NFC_FORUM_RTD) {
//                            String value = new String(payload);
//                            System.out.println(value);
//                        }
                        }
                    }
                } else if (contains(connectionTypes, ISO14443Connection.class)) {
                } else if (contains(connectionTypes, PlainTagConnection.class)) {
                }
            } catch (ContactlessException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                if (conn != null) {
                    try {conn.close();} catch (IOException ex) {}
                }
            }
        }

    }

    public String getTag() {
        return tag;
    }

    public void setTag(String value) {
        tag = value;
    }

    public int hashCode() {
        int hash = 7;
        hash = 53 * hash + (this.tag != null ? this.tag.hashCode() : 0);
        return hash;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o instanceof TagObject) {
            TagObject other = (TagObject) o;
            if (getType() == other.getType()) {
                if (tag.equals(other.getTag())) {
                    return true;
                }
            }
        }
        return false;
    }

    public String getUrl() {
        return this.properties[0].getUrl();
    }

    public String getUri() {
        return uri;
    }

    public void recordDetected(NDEFMessage arg0) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private boolean contains(Object[] array, Object target) {
        for (int i = 0; i < array.length; i++) {
            if (target.equals(array[i])) {
                return true;
            }
        }
        return false;
    }
}
